<?php

class EmployeeTable extends Doctrine_Table
{
    public function getActiveEmployees(){
        $q = $this->getActiveEmployeesQuery();
        return $q->execute();
    }
    public function getActiveEmployeesQuery(){
        $q = $this->createQuery('e')
                ->innerJoin('e.Positions p')
                ->where('p.endDate is NULL');

        return $q;
    }
    public function getActiveEmployeesForPayPeriod(PayPeriod $pay_period){

        $range = array($pay_period->getEndDate(),
            $pay_period->getStartDate());
        $q = $this->createQuery('e')
                ->innerJoin('e.Positions p')
                ->where('p.payment_periodicity_id = ?', $pay_period->getPaymentPeriodicityId())
                ->andWhere('p.startDate <= ?',$pay_period->getEndDate())
                ->andWhere('p.endDate IS NULL OR (p.endDate <= ? AND p.endDate >= ?)', $range);

//        var_dump($range);
//        echo $q->getSqlQuery() . '\n';
        return $q->execute();
    }
    public function getInactiveEmployees(){
        $q = $this->getInactiveEmployeesQuery();
        return $q->execute();

    }
    public function getInactiveEmployeesQuery($employeeId =  null){
        $q1 = $q = $this->createQuery()
                ->select('e.id')
                ->from('Employee e')
                ->innerJoin('e.Positions p')
                ->where('p.endDate is NULL');
        $ids = $q1->fetchArray();
        $nids = array();
        $c=0;
        foreach ($ids as $id) {
            $nids[$c] = $id['id'];
            $c++;
        }
        //var_dump($nids);
        $q = $this->createQuery('e')
                ->leftJoin('e.Positions p')
                ->whereNotIn('e.id', $nids)
                ->orderBy('e.lastname, e.lastname2, e.firstname');
        if($employeeId != null){
            $q->orWhere('e.id = ?', $employeeId);
        }
        return $q;
    }
}